package com.tucc.hello.leetcode.no1_400.no1_200.no81_100.no91;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.numDecodings("10"));
    }


    public int numDecodings(String s) {
        int[] dp = new int[s.length() + 1];
        dp[1] = s.charAt(0) == '0' ? 0 : 1;
        if (dp[1] == 0) {
            return 0;
        }
        dp[0] = 1;
        for (int i = 2; i <= s.length(); i++) {
            char ch = s.charAt(i - 1);
            char lastCh = s.charAt(i - 2);
            if (ch == '0') {
                if (lastCh > '2' || lastCh == '0') {
                    return 0;
                }
                dp[i] = dp[i - 2];
            } else if (lastCh == '1' || (lastCh == '2' && ch <= '6')) {
                dp[i] = dp[i - 1] + dp[i - 2];
            } else {
                dp[i] = dp[i - 1];
            }
        }
        return dp[s.length()];
    }

}
